using System.IO;
using System.Text;
using System.Utility.System;

namespace System.Utility
{
	public class ConsoleWriter : TextWriter
	{
		public override Encoding Encoding
		{
			get
			{
				return Encoding.UTF8;
			}
		}

		public Action<string> OnWirte
		{
			get;
			set;
		}

		public ConsoleWriter()
		{
		}

		private void DoWrite(object value)
		{
			if (OnWirte != null)
			{
				OnWirte(TypeExtension.ToSafeString(value));
			}
		}

		private void DoWrite(string value)
		{
			if (OnWirte != null)
			{
				OnWirte(value);
			}
		}

		private void DoWriteLine(object value)
		{
			if (OnWirte != null)
			{
				OnWirte(string.Concat(Environment.NewLine, TypeExtension.ToSafeString(value)));
			}
		}

		private void DoWriteLine(string value)
		{
			if (OnWirte != null)
			{
				OnWirte(string.Concat(Environment.NewLine, value));
			}
		}

		public override void Write(char value)
		{
			DoWrite(value);
		}

		public override void Write(bool value)
		{
			DoWrite((value ? "true" : "false"));
		}

		public override void Write(int value)
		{
			DoWrite(value);
		}

		public override void Write(uint value)
		{
			DoWrite(value);
		}

		public override void Write(long value)
		{
			DoWrite(value);
		}

		public override void Write(ulong value)
		{
			DoWrite(value);
		}

		public override void Write(float value)
		{
			DoWrite(value);
		}

		public override void Write(double value)
		{
			DoWrite(value);
		}

		public override void Write(decimal value)
		{
			DoWrite(value);
		}

		public override void Write(string value)
		{
			DoWrite(value);
		}

		public override void Write(object value)
		{
			DoWrite(value);
		}

		public override void Write(string format, params object[] arg)
		{
			DoWrite(string.Format(format, arg));
		}

		public override void WriteLine()
		{
			DoWriteLine(string.Empty);
		}

		public override void WriteLine(char value)
		{
			DoWriteLine(value);
		}

		public override void WriteLine(bool value)
		{
			DoWriteLine(value);
		}

		public override void WriteLine(int value)
		{
			DoWriteLine(value);
		}

		public override void WriteLine(uint value)
		{
			DoWriteLine(value);
		}

		public override void WriteLine(long value)
		{
			DoWriteLine(value);
		}

		public override void WriteLine(ulong value)
		{
			DoWriteLine(value);
		}

		public override void WriteLine(float value)
		{
			DoWriteLine(value);
		}

		public override void WriteLine(double value)
		{
			DoWriteLine(value);
		}

		public override void WriteLine(decimal value)
		{
			DoWriteLine(value);
		}

		public override void WriteLine(string value)
		{
			DoWriteLine(value);
		}

		public override void WriteLine(object value)
		{
			DoWriteLine(value);
		}

		public override void WriteLine(string format, params object[] arg)
		{
			DoWriteLine(string.Format(format, arg));
		}
	}
}